package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.*;
import com.xidian.zhiancloud.dao.syset.PositionMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.PositionList;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.equipFacility.DefendLedgerQO;
import com.xidian.zhiancloud.qo.equipFacility.DefendRegisterQO;
import com.xidian.zhiancloud.vo.equipFacility.DefendLedgerVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendPlanMakeVO;
import com.xidian.zhiancloud.vo.equipFacility.DefendRegisterVO;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Summerless
 * @Date: 2021/4/3 22:20
 */
@Service
public class DefendLedgerService extends ServiceImpl<DefendLedgerMapper, DefendLedger> implements IService<DefendLedger> {

    @Autowired
    private DefendRegisterMapper defendRegisterMapper;

    @Autowired
    private OrganizationMapper organizationMapper;

    @Autowired
    private DefendTypeMapper defendTypeMapper;

    @Autowired
    private DeviceTypeMapper deviceTypeMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private PositionMapper positionMapper;

    @Autowired
    private EqFacMngMapper eqFacMngMapper;

    /**
     * 列表查询
     * @param defendRegisterQO
     * @return
     */
    public IPage<DefendRegisterVO> getListByQO(DefendRegisterQO defendRegisterQO) {
        Page<DefendRegisterVO> page = new Page<>(defendRegisterQO.getCurrent(), defendRegisterQO.getSize());

        IPage<DefendRegisterVO> list = defendRegisterMapper.selectByQO(page,defendRegisterQO);

        List<DefendRegisterVO> records = list.getRecords();
        List<DefendRegisterVO> new_list = new ArrayList<>();

        for (DefendRegisterVO record : records) {

            if(record.getStatus() == 1){

                Organization organization = organizationMapper.selectById(record.getMngCtrDeptId());
                record.setDepartmentName(organization == null ? "该部门不存在" : organization.getName());

                DefendType defendType = defendTypeMapper.selectById(record.getDefendTypeId());
                record.setDefendTypeName(defendType == null ? "该类型不存在" : defendType.getName());

                DeviceType deviceType = deviceTypeMapper.selectById(record.getDeviceTypeId());
                record.setDeviceName(deviceType == null ? "该类型不存在" : deviceType.getName());

                String ids = record.getDefenderIds();
                long[] array = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).toArray();
                String name = "";
                for (long id : array) {
                    SysUser sysUser = sysUserMapper.selectById(id);
                    if(sysUser != null){
                        name += sysUser.getName() + ",";
                    }
                }
                record.setDefenderName(name);

                new_list.add(record);
            }
        }
        list.setRecords(new_list);

        //排序
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);

        return list;
    }

    /**
     * 查看
     * @param id
     * @return
     */
    public DefendLedgerVO getById(Long id){

        DefendRegister defendRegister = defendRegisterMapper.selectById(id);

        DefendLedgerVO vo = new DefendLedgerVO();
        BeanUtils.copyProperties(defendRegister,vo);

        EqFacMng eqFacMng = eqFacMngMapper.selectById(vo.getDeviceId());
        vo.setDeviceStatus(eqFacMng.getEquipStatus());
        vo.setDeviceName(eqFacMng.getName());
        vo.setModel(eqFacMng.getModel());

        DeviceType deviceType = deviceTypeMapper.selectById(eqFacMng.getDeviceTypeId());
        vo.setDeviceName(deviceType == null ? "该类型不存在" : deviceType.getName());

        Organization organization = organizationMapper.selectById(eqFacMng.getMngCtrDeptId());
        vo.setDepartmentName(organization == null ? "该部门不存在" : organization.getName());

        PositionList positionList = positionMapper.selectById(eqFacMng.getPosition());
        vo.setPositionName(positionList == null ? "该区域不存在" : positionList.getName());

        DefendType defendType = defendTypeMapper.selectById(vo.getDefendTypeId());
        vo.setDefendTypeName(defendType == null ? "该类型不存在" : defendType.getName());

        String ids = vo.getDefenderIds();
        long[] array = Arrays.stream(ids.split(",")).mapToLong(Long::parseLong).toArray();
        String name = "";
        for (long i : array) {
            SysUser sysUser = sysUserMapper.selectById(i);
            if(sysUser != null){
                name += sysUser.getName() + ",";
            }
        }

        vo.setDefendersName(name);
        return vo;
    }
}
